package com.spring.sunflower.dialog

import android.content.Context
import android.os.CountDownTimer
import android.os.Handler
import android.os.Looper
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.*
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.google.android.flexbox.FlexboxLayout
import com.lxj.xpopup.core.CenterPopupView
import com.makeramen.roundedimageview.RoundedImageView
import com.spring.sunflower.R
import com.spring.sunflower.util.AppUtils
import com.spring.sunflower.util.DisplayUtils
import com.spring.sunflower.util.DoubleClickUtils
import java.util.*

/**
 * Created by Kevin on 2021/9/26<br/>
 *
 * Describe:<br/>
 */
class BindPhoneDialog(
    context: Context,
) : CenterPopupView(context) {
    private var etPhoneNum: EditText? = null
    private var etCode: EditText? = null
    private var tvDone: TextView? = null
    private var tvGetCode: TextView? = null
    private var countDownTimer: CountDownTimer? = null
    private var isOnTick = false
    private var ivClose: ImageView? = null

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_bind_phone
    }

    override fun onCreate() {
        super.onCreate()
        etPhoneNum = findViewById(R.id.etPhone)
        etCode = findViewById(R.id.etPhoneCode)
        tvDone = findViewById(R.id.tvDone)
        tvGetCode = findViewById(R.id.tvGetCode)
        ivClose = findViewById(R.id.ivClose)
        listener?.onCreateView()
        etPhoneNum!!.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable) {
                if (etPhoneNum!!.getText().toString().trim { it <= ' ' }.length == 11) {
                    tvGetCode?.setTextColor(AppUtils.getColorX(context, R.color.colorPrimary))
                } else {
                    if (isOnTick) return
                    tvGetCode?.setTextColor(AppUtils.getColorX(context, R.color.gray_4))
                }
            }
        })
        ivClose?.setOnClickListener {
            countDownTimer?.cancel()
            dismiss()
        }
        tvGetCode?.setOnClickListener {
            if (isOnTick) return@setOnClickListener
            val mPhoneNum = etPhoneNum!!.text.toString().trim { it <= ' ' }
            if (TextUtils.isEmpty(mPhoneNum)) {
                ToastUtils.showShort("手机号不能为空")
                return@setOnClickListener
            }
            if (mPhoneNum.length != 11 || !AppUtils.isValidPhoneNum(mPhoneNum)) {
                ToastUtils.showShort("请输入正确手机号")
            } else {
                listener?.onGetCodeClick(mPhoneNum)
                startCountDown()
            }
        }
        tvDone?.setOnClickListener {
            if (DoubleClickUtils.isFastLongClick()) return@setOnClickListener
            val code = etCode!!.getText().toString().trim()
            val phoneNum = etPhoneNum!!.text.toString().trim()
            if (TextUtils.isEmpty(phoneNum)) {
                ToastUtils.showShort("手机号不能为空")
                return@setOnClickListener
            }
            if (phoneNum.length != 11 || !AppUtils.isValidPhoneNum(phoneNum)) {
                ToastUtils.showShort("请输入正确手机号")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(code)) {
                ToastUtils.showShort("验证码不能为空")
                return@setOnClickListener
            }
            if (code.length != 4) {
                ToastUtils.showShort("验证码位数不够")
                return@setOnClickListener
            }
            listener?.onBindClick(phoneNum, code)
            countDownTimer?.cancel()
            dismiss()
        }
    }

    private var listener: OnBindPhoneListener? = null
    fun setOnBindPhoneListener(l: OnBindPhoneListener) {
        listener = l
    }

    interface OnBindPhoneListener {
        fun onBindClick(phoneNum:String,code:String)//
        fun onGetCodeClick(phoneNum: String)
        fun onCreateView()
    }


    override fun getMaxWidth(): Int {
        return (DisplayUtils.getScreenWidth(context) * 0.8).toInt()
    }

    private fun startCountDown() {
        if (isOnTick) {
            return
        }
        isOnTick = true
        countDownTimer = object : CountDownTimer(60000, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                tvGetCode!!.text = "倒计时" + millisUntilFinished / 1000 + "s"
            }

            override fun onFinish() {
                isOnTick = false
                tvGetCode!!.text = "重新获取"
            }
        }
        countDownTimer?.start()
    }
}